---@diagnostic disable: lowercase-global, undefined-global, trailing-space

local modid = 'yuemiheng'
---comment
---@param doer ent
---@param str string
local function shortmsg(doer,str)
    if doer.components.talker then
        doer.components.talker:Say(str)
    end
end

---@type data_keyhandler[]
local data = {
    {
        namespace = modid,
        skillid = 'ymh_switch_spear_staff',
        type = 'down',
        avatar = {'yuemiheng'},
        key = 'KEY_T',
        fn = function (player)
            if not player:HasTag("ghost") and player.components.inventory then
                local spears = player.components.inventory:GetItemsWithTag('yuemiheng_spear')
                local staffs = player.components.inventory:GetItemsWithTag('yuemiheng_staff')

                for _,v in pairs(spears) do
                    local tf_staff = SpawnPrefab('yuemiheng_staff')
                    -- save
                    tf_staff.ymh_spear_upgrade = deepcopy(v.ymh_spear_upgrade)
                    tf_staff.is_ymh_spear_upgrade = v.is_ymh_spear_upgrade
                    if v.components.finiteuses then
                        local cur_percent = v.components.finiteuses:GetPercent()
                        tf_staff.ymh_spear_finiteuse =  cur_percent
                    end
                    -- load
                    if v.ymh_staff_upgrade and v.is_ymh_staff_upgrade then
                        tf_staff.ymh_staff_upgrade = deepcopy(v.ymh_staff_upgrade)
                        tf_staff.is_ymh_staff_upgrade = v.is_ymh_staff_upgrade
                    end
                    -- upgrade
                    if tf_staff.is_ymh_staff_upgrade then
                        if not tf_staff:HasTag('is_ymh_staff_upgrade') then
                            tf_staff:AddTag('is_ymh_staff_upgrade')
                        end
                        if tf_staff.components.finiteuses then
                            tf_staff:RemoveComponent('finiteuses')
                        end
                    end
                    if v.ymh_staff_finiteuse then
                        if tf_staff.components.finiteuses then
                            tf_staff.components.finiteuses:SetPercent(v.ymh_staff_finiteuse)
                        end
                    end
                    
                    v:Remove()
                    player.components.inventory:GiveItem(tf_staff)
                end

                for _,v in pairs(staffs) do
                    local tf_spear = SpawnPrefab('yuemiheng_spear')
                    -- save
                    tf_spear.ymh_staff_upgrade = deepcopy(v.ymh_staff_upgrade)
                    tf_spear.is_ymh_staff_upgrade = v.is_ymh_staff_upgrade
                    if v.components.finiteuses then
                        local cur_percent = v.components.finiteuses:GetPercent()
                        tf_spear.ymh_staff_finiteuse =  cur_percent
                    end
                    -- load
                    if v.ymh_spear_upgrade and v.is_ymh_spear_upgrade then
                        tf_spear.ymh_spear_upgrade = deepcopy(v.ymh_spear_upgrade)
                        tf_spear.is_ymh_spear_upgrade = v.is_ymh_spear_upgrade
                    end
                    -- upgrade
                    if tf_spear.is_ymh_spear_upgrade then
                        if not tf_spear:HasTag('is_ymh_spear_upgrade') then
                            tf_spear:AddTag('is_ymh_spear_upgrade')
                        end
                        if tf_spear.components.finiteuses then
                            tf_spear:RemoveComponent('finiteuses')
                        end
                    end
                    if v.ymh_spear_finiteuse then
                        if tf_spear.components.finiteuses then
                            tf_spear.components.finiteuses:SetPercent(v.ymh_spear_finiteuse)
                        end
                    end

                    v:Remove()
                    player.components.inventory:GiveItem(tf_spear)
                end
            end
        end
    },
    {
        namespace = modid,
        skillid = 'ymh_fn_staff_onoff',
        type = 'down',
        avatar = {'yuemiheng'},
        key = TUNING[string.upper('CONFIG_'..modid..'_staff_allow_cast_key')],
        fn = function (player)
            if not player:HasTag("ghost") then
                -- local staff = player.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
                if player:HasTag('ymh_allow_staff_cast') then
                    player:RemoveTag('ymh_allow_staff_cast')
                    shortmsg(player,' staff cast off')
                else
                    player:AddTag('ymh_allow_staff_cast')
                    shortmsg(player,' staff cast on')
                end

                -- for _,v in pairs(staffs or {}) do
                --     print(v)
                --     if v:HasTag('nomagic') then
                --         v:RemoveTag('nomagic')
                --         shortmsg(player,'remove')
                --     else
                --         v:AddTag('nomagic')
                --     end
                -- end
            end
        end,
    }
}


return data